/*
 * cdma_simware.h -- header file of functions for cdma_simwaretrum gsm module
 *
 * Copyright (C) 2007 lufang <lufang@gmail.com>
 *
 * Version 1.0 june 2007
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/modemcontrol.h>

#include "config.h"
#include "gsm0710.h"
#include "buffer.h"
#include "utils.h"
#include "gsm_module.h"

#define MTK_MAX_FRAME_SIZE				256
#define MTK_READY_COMMAND				"+EIND: 128\r\n"

#define MTK_MODULE_DEVICE_NAME 			"/dev/modemcontrol"

enum
{
	CHANNEL_VIRTUIAL,
	CHANNEL_DATA,
	CHANNEL_URC,
	CHANNEL_AT_COMMAND,
};


static gboolean cdma_simware_module_open_mux (GsmModule* thiz)
{
	return TRUE;
}

static gboolean cdma_simware_module_restart (GsmModule* thiz)
{
	
	return TRUE;
}

static gboolean cdma_simware_module_destroy (GsmModule* thiz)
{

	g_free (thiz);
	
	return TRUE;	
}

static gboolean cdma_simware_module_close (GsmModule* thiz)
{
	return TRUE;
}

static gboolean cdma_simware_module_startup (GsmModule* thiz)
{
	return TRUE;
}

static int cdma_simware_write_ussp_data (GsmModule* thiz, int channel, const char* input, int count)
{
	int port = 0;
	int wrote = 0;

	if (channel == CHANNEL_AT_COMMAND)
	{
		port = 0;
	}
	else
	{
		port = 1;
	}

	wrote = write(thiz->vir_ports.ussp_fds[port], input, count);

	return wrote;
}

static int cdma_simware_extract_frames (GsmModule* thiz, GSM0710_Buffer* buf)
{
	char buffer[1024] = {0};
	int readed = 0;

	readed = gsm0710_buffer_read (buf, buffer, 1024);
	cdma_simware_write_ussp_data (thiz, CHANNEL_AT_COMMAND, buffer, readed);

	return readed;
}

static int cdma_simware_write_frame (GsmModule* thiz, int channel, const char *input, int count, unsigned char type) 
{
	int wrote = 0;

	wrote = write (thiz->serial_fd, input, count);

	return;
}

static void cdma_simware_param_negotiation (GsmModule* thiz, int channel)
{
	return;
}

static GsmModule* cdma_simware_module_create (GsmModuleGenerator* generator, ModuleConfig* config)
{
	GsmModule* thiz = g_malloc0 (sizeof (GsmModule));

	module_config_copy (&thiz->module_config, config);
	
	thiz->module_restart = cdma_simware_module_restart;
	thiz->module_destroy = cdma_simware_module_destroy;
	thiz->module_open_mux = cdma_simware_module_open_mux;
	thiz->module_startup = cdma_simware_module_startup;
	thiz->module_close = cdma_simware_module_close;

	thiz->extract_frames = cdma_simware_extract_frames;
	thiz->write_frame = cdma_simware_write_frame;
	thiz->write_ussp_data = cdma_simware_write_ussp_data;
	thiz->param_negotiation = cdma_simware_param_negotiation;

	return thiz;
}

static gboolean cdma_simware_generator_match_type (GsmModuleGenerator* generator, const char* type)
{
	return (strcmp (type, "cdma_simware") == 0);
}

static void cdma_simware_generator_destroy (GsmModuleGenerator* generator)
{
	g_free (generator);
	
	return;
}

GsmModuleGenerator* get_generator ()
{
	GsmModuleGenerator* generator = g_malloc0 (sizeof (GsmModuleGenerator));

	generator->create_module_func = cdma_simware_module_create;
	generator->match_func = cdma_simware_generator_match_type;
	generator->destroy_func = cdma_simware_generator_destroy;

	return generator;
}


